package com.xuecheng.order.service;

import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.framework.domain.task.XcTaskHis;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.order.dao.XcTaskHistoryRepositoty;
import com.xuecheng.order.dao.XcTaskRepository;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author: gongfy
 * @create: 2020/9/24 16:13
 * @Description:
 */
@Service
public class XcTaskService {

    @Autowired
    private XcTaskRepository xcTaskRepository;
    @Autowired
    private XcTaskHistoryRepositoty xcTaskHistoryRepositoty;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //取出前n条任务,取出指定时间之前处理的任务
    public List<XcTask> findXcTaskByUpdateTimeBefore(Date updateTime,int size){
        //设置分页参数，取出前size条记录
        Pageable pageable = new PageRequest(0,size);
        Page<XcTask> page = xcTaskRepository.findXcTasksByUpdateTimeBefore(pageable, updateTime);
        List<XcTask> xcTaskList = page.getContent();
        return xcTaskList;
    }

    /**
    * @Author: gongfy
    * @Param: xcTask 任务对象, exchange 交换机id, routingKey 路由key
    * @Date: 2020/9/24 17:01
    * @Description:  发送消息并更新updateTime
    **/
    public void sendMsgAndUpdateTime(XcTask xcTask,String exchange,String routingKey){
        //1.先查询任务
        Optional<XcTask> optionalXcTask = xcTaskRepository.findById(xcTask.getId());
        if (optionalXcTask.isPresent()){
            rabbitTemplate.convertAndSend(exchange,routingKey,xcTask);//发送消息
            XcTask one = optionalXcTask.get();
            one.setUpdateTime(new Date());//更新当前时间
            xcTaskRepository.save(one);
        }
    }

    /**
    * @Author: gongfy
    * @Param:
    * @Date: 2020/9/25 10:51
    * @Description: 更新任务版本
    **/
    @Transactional  // 不加会报javax.persistence.TransactionRequiredException
    public int updateTaskVersion(String taskId,Integer taskVersion){
        return xcTaskRepository.updateTaskVersion(taskId, taskVersion);
    }

    @Transactional
    public void finishTask(String xccTaskId){
        // 先查询task表如果有任务，则1.添加到task_his表 2.移除task表数据
        Optional<XcTask> optionalXcTask = xcTaskRepository.findById(xccTaskId);
        if (optionalXcTask.isPresent()) {
            XcTask xcTask = optionalXcTask.get();
            XcTaskHis xcTaskHis = new XcTaskHis();
            BeanUtils.copyProperties(xcTask,xcTaskHis);
            xcTaskHistoryRepositoty.save(xcTaskHis);
            xcTaskRepository.delete(xcTask);
        }
    }









}
